﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ECCSystem.BizRule;
using System.Text.RegularExpressions;

namespace ECCSystem
{
	public partial class frmDefect : Form
	{
		int intEditMode = 0;

		public frmDefect()
		{
			InitializeComponent();
		}

		private void btnSave_Click(object sender, EventArgs e)
		{
			if (Check() == false)
			{
				return;
			}
			ClsDefect objClsDefect = new ClsDefect();
			if (intEditMode == 0)
			{
				objClsDefect.AddDefect(dtpDefectTime.Value, Convert.ToInt32(cmbUser.SelectedValue), Convert.ToInt32(txtNumber.Text));
			}
			else
			{
				objClsDefect.UpdateDefect(dtvUser.SelectedRows[0].Cells["Defect_KeyID"].Value.ToString(),
											dtpDefectTime.Value, Convert.ToInt32(cmbUser.SelectedValue), Convert.ToInt32(txtNumber.Text));
				intEditMode = 0;						
			}

			this.btnDelete.Enabled = true;
			this.btnUpdata.Enabled = true;
			this.dtvUser.Enabled = true;
			this.txtNumber.Text = "";
			this.dtpDefectTime.Value = DateTime.Now;
			DataSet ds = (DataSet)objClsDefect.GetList(this.dtpDefectTime.Value, 0, 0, 0);
			BindData(ds);
		}

		private bool Check()
		{
			if (intEditMode != 2)
			{
				if (txtNumber.Text.Trim() == "")
				{
					MessageBox.Show("不備数は空白不可!", "ECC Information");
					return false;
				}


				if (!Regex.IsMatch(txtNumber.Text.Trim(), "^\\d{1,}$"))
				{
					MessageBox.Show("不備数入力不正確!(正整数)", "ECC Information");
					return false;
				}

				if (cmbUser.Text.Trim() == "")
				{
					MessageBox.Show("作業者は空白不可!", "ECC Information");
					return false;
				}

				
				for (int i = 0; i < dtvUser.Rows.Count; i++)
				{
					if (intEditMode == 0)
					{
						if (cmbUser.Text.Trim() == dtvUser.Rows[i].Cells["Defect_UserName"].Value.ToString())
						{
							MessageBox.Show("作業者重複!", "ECC Information");
							return false;
						}
					}
					else
					{
						if (cmbUser.Text.Trim() == dtvUser.Rows[i].Cells["Defect_UserName"].Value.ToString() && i != dtvUser.CurrentRow.Index)
						{
							MessageBox.Show("作業者重複!", "ECC Information");
							return false;
						}
					}
				}

				return true;
			}

			return true;
		}

		private void frmDefect_Load(object sender, EventArgs e)
		{
			ClsUserMaintenance objClsUserMaintenance = new ClsUserMaintenance();
			
			DataSet ds = (DataSet)objClsUserMaintenance.GetList();
			BindCmb(ds);
			ClsDefect objClsDefect = new ClsDefect();
			ds = null;
			ds = (DataSet)objClsDefect.GetList(this.dtpDefectTime.Value, 0, 0, 0);
			BindData(ds);
		}

		private void BindData(DataSet ds)
		{
			dtvUser.DataSource = null;
			if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
			{
				dtvUser.AutoGenerateColumns = false;
				dtvUser.DataSource = ds.Tables[0];

				if (dtvUser.Rows.Count > 0)
				{
					this.btnDelete.Enabled = true;
					this.btnUpdata.Enabled = true;
					this.dtvUser.Enabled = true;
					this.btnAdd.Enabled = true;
					this.btnCancel.Enabled = false;
					this.btnSave.Enabled = false;
				}
				else
				{
					this.btnDelete.Enabled = false;
					this.btnUpdata.Enabled = false;
					this.dtvUser.Enabled = true;
					this.btnAdd.Enabled = true;
					this.btnCancel.Enabled = false;
					this.btnSave.Enabled = false;
				}

				dtpDefectTime.Value = Convert.ToDateTime(dtvUser.Rows[0].Cells["Defect_Date"].Value);
				txtNumber.Text = Convert.ToString(dtvUser.Rows[0].Cells["Defect_number"].Value);
				cmbUser.SelectedValue = Convert.ToString(dtvUser.Rows[0].Cells["Defect_UserID"].Value);
			}
		}

		private void BindCmb(DataSet ds)
		{

			if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
			{
				DataTable dt = ds.Tables[0];
				DataRow dr = dt.NewRow();
				dr[0] = System.DBNull.Value;
				dr[1] = " ";
				dt.Rows.InsertAt(dr, 0);
				cmbUser.ValueMember = "ID";
				cmbUser.DisplayMember = "UserName";
				cmbUser.DataSource = dt;
				
			}
		}

		private void btnSearch_Click(object sender, EventArgs e)
		{
			if (txtNumber.Text.Trim() != "")
			{
				if (!Regex.IsMatch(txtNumber.Text.Trim(), "^\\d{1,}$"))
				{
					MessageBox.Show("不備数入力不正確!(正整数)", "ECC Information");
					return;
				}
			}

			ClsDefect objClsDefect = new ClsDefect();
			DataSet ds = new DataSet();
			if (cmbUser.Text.Trim() != "" && txtNumber.Text.Trim() == "")
			{
				ds = (DataSet)objClsDefect.GetList(this.dtpDefectTime.Value, Convert.ToInt32(cmbUser.SelectedValue), 1, 0);
			}
			else if (cmbUser.Text.Trim() == "" && txtNumber.Text.Trim() == "")
			{
				ds = (DataSet)objClsDefect.GetList(this.dtpDefectTime.Value, 0, 0, 0);
			}
			else if (cmbUser.Text.Trim() == "" && txtNumber.Text.Trim() != "")
			{
				ds = (DataSet)objClsDefect.GetList(this.dtpDefectTime.Value, 0, 2,Convert.ToInt32(txtNumber.Text.Trim()));
			}
			else if (cmbUser.Text.Trim() != "" && txtNumber.Text.Trim() != "")
			{
				ds = (DataSet)objClsDefect.GetList(this.dtpDefectTime.Value, Convert.ToInt32(cmbUser.SelectedValue), 3, Convert.ToInt32(txtNumber.Text.Trim()));
			}

			BindData(ds);
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			dtpDefectTime.Value = DateTime.Now;
			txtNumber.Text = "";
			cmbUser.SelectedValue = System.DBNull.Value;

			if (dtvUser.Rows.Count > 0)
			{
				this.btnDelete.Enabled = true;
				this.btnUpdata.Enabled = true;
				this.dtvUser.Enabled = true;
				this.btnAdd.Enabled = true;
				this.btnCancel.Enabled = false;
				this.btnSave.Enabled = false;

				dtvUser.Rows[0].Selected = true;

				dtpDefectTime.Value = Convert.ToDateTime(dtvUser.CurrentRow.Cells["Defect_Date"].Value);
				txtNumber.Text = Convert.ToString(dtvUser.CurrentRow.Cells["Defect_number"].Value);
				cmbUser.SelectedValue = Convert.ToString(dtvUser.CurrentRow.Cells["Defect_UserID"].Value);
			}
			else
			{
				this.btnDelete.Enabled = false;
				this.btnUpdata.Enabled = false;
				this.dtvUser.Enabled = true;
				this.btnAdd.Enabled = true;
				this.btnCancel.Enabled = false;
				this.btnSave.Enabled = false;
			}
		}

		private void btnDelete_Click(object sender, EventArgs e)
		{
			//wether have selected row,if have not selected row, pop-up prompt
			if (dtvUser.CurrentRow == null)
			{
				MessageBox.Show("データを選んでください！", "ECC Information");
				return;
			}
			else
			{
				if (MessageBox.Show("削除します、よろしいですか？", "ECC Information", MessageBoxButtons.OKCancel) == DialogResult.OK)
				{
					ECCSystem.BizRule.ClsDefect objBRMC = new ECCSystem.BizRule.ClsDefect();
					objBRMC.DeleteDefect(Convert.ToString(dtvUser.SelectedRows[0].Cells["Defect_KeyID"].Value));
					DataSet ds = (DataSet)objBRMC.GetList(this.dtpDefectTime.Value, 0, 0, 0);
					BindData(ds);
					dtpDefectTime.Value = DateTime.Now;
					txtNumber.Text = "";
					cmbUser.SelectedValue = System.DBNull.Value;
				}

			}
		}

		private void btnUpdata_Click(object sender, EventArgs e)
		{
			if (dtvUser.CurrentRow == null)
			{
				MessageBox.Show("データを選んでください！", "ECC Information");
				return;
			}

			//dtpDefectTime.Value = Convert.ToDateTime(dtvUser.SelectedRows[0].Cells["Defect_Date"].Value);
			//txtNumber.Text = Convert.ToString(dtvUser.SelectedRows[0].Cells["Defect_number"].Value);
			//cmbUser.SelectedValue = Convert.ToString(dtvUser.SelectedRows[0].Cells["Defect_UserID"].Value);
			this.btnDelete.Enabled = false;
			this.btnUpdata.Enabled = false;
			this.dtvUser.Enabled = false;
			this.btnAdd.Enabled = false;
			this.btnCancel.Enabled = true;
			this.btnSave.Enabled = true;
			this.dtpDefectTime.Value = DateTime.Now;
			intEditMode = 1;
		}

		private void frmDefect_FormClosed(object sender, FormClosedEventArgs e)
		{
			Form frmMain = new frmMain();
			frmMain.Show();
		}

		private void dtvUser_CellClick(object sender, DataGridViewCellEventArgs e)
		{
			dtpDefectTime.Value = DateTime.Now;
			txtNumber.Text = "";
			cmbUser.SelectedValue = System.DBNull.Value;

			if (dtvUser.Rows.Count <= 0)
			{
				return;
			}

			dtpDefectTime.Value = Convert.ToDateTime(dtvUser.SelectedRows[0].Cells["Defect_Date"].Value);
			txtNumber.Text = Convert.ToString(dtvUser.SelectedRows[0].Cells["Defect_number"].Value);
			cmbUser.SelectedValue = Convert.ToString(dtvUser.SelectedRows[0].Cells["Defect_UserID"].Value);
		}

		private void btnAdd_Click(object sender, EventArgs e)
		{
			dtpDefectTime.Value = DateTime.Now;
			txtNumber.Text = "";
			cmbUser.SelectedValue = System.DBNull.Value;

			this.btnDelete.Enabled = false;
			this.btnUpdata.Enabled = false;
			this.dtvUser.Enabled = false;
			this.btnAdd.Enabled = false;
			this.btnCancel.Enabled = true;
			this.btnSave.Enabled = true;

			intEditMode = 0;
		}

		private void btnFlash_Click(object sender, EventArgs e)
		{
			ClsUserMaintenance objClsUserMaintenance = new ClsUserMaintenance();

			DataSet ds = (DataSet)objClsUserMaintenance.GetList();
			BindCmb(ds);
			ClsDefect objClsDefect = new ClsDefect();
			ds = null;
			ds = (DataSet)objClsDefect.GetList(this.dtpDefectTime.Value, 0, 0, 0);
			BindData(ds);
		}
	}
}